package com.ljh.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljh.mapper.SongMapper;
import com.ljh.pojo.SList;
import com.ljh.pojo.Singer;
import com.ljh.pojo.Song;
import com.ljh.pojo.list;
import com.ljh.service.CollectionService;
import com.ljh.service.ListService;
import com.ljh.service.SongService;
import com.ljh.service.listServ;
import com.ljh.utils.Result;
import com.ljh.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.Data;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("list")
@Api(description = "歌单接口管理")
@CrossOrigin
public class ListController {

    @Autowired
    ListService listService;

    @Autowired
    CollectionService collectionService;

    @Autowired
    listServ listServ;

    @Autowired
    SongService songService;

    /**
     * 通过ListId来查找歌曲
     *
     * @param listId
     * @return
     */
    @GetMapping("findSongByListId")
    @ApiOperation(value = "通过listId来查找歌曲")
    public Result findSongByListId(Integer listId) {
        System.out.println(listId);
        return Result.success(listService.findSongByListId(listId));
    }

    /**
     * 通过专辑名称来查找歌曲
     *
     * @param CollId
     * @return
     */
    @GetMapping("/toSongList")
    @ApiOperation(value = "通过id查找id")
    public Result findById(Integer CollId) {
        System.out.println(CollId);
        CollectionSongVo collSong = collectionService.findCollSong(CollId);
        return Result.success(collSong);
    }

    /**
     * 热歌推荐
     *
     * @return
     */
    @GetMapping("/PerferList")
    @ApiOperation("喜欢列表")
    public Result findPerfer() {
        List<SList> perfer = listService.findPerfer();

        return Result.success(perfer);
    }

    /**
     * 随机查找
     *
     * @return
     */
    @GetMapping("findRandom")
    @ApiOperation("随机查找")
    public Result findRandom() {
        List<SList> random = listService.findRandom();
        return Result.success(random);
    }

    /**
     * 为页面里面随机查找三个
     */
    @GetMapping("findThree")
    @ApiOperation(value = "为页面里面随机查找三个")
    public Result findThree(Integer id) {
        List<SList> three = listService.findThree(id);
        return Result.success(three);
    }

    /**
     * 为我的页面里面查找收藏表单
     *
     * @param id
     * @return
     */
    @GetMapping("findMyListLike")
    @ApiOperation(value = "为我的页面里面查找收藏表单")
    public Result findMyListLike(Integer id) {
        List<MyListLike> myListLike = listService.findMyListLike(id);
        return Result.success(myListLike);
    }

    /**
     * 通过用户id来查找建立的歌单
     *
     * @param listId
     * @return
     */
    @GetMapping("findListById")
    @ApiOperation(value = "通过用户id来查找建立的歌单")
    public Result findListById(Integer listId) {
        List<SList> listById = listService.findListById(listId);
        return Result.success(listById);
    }

    /**
     * 通过用户id来查找收藏歌单
     *
     * @param listLikeId
     * @return
     */
    @GetMapping("findListLikeById")
    @ApiOperation(value = "通过用户id来查找收藏歌单")
    public Result findListLikeById(Integer listLikeId) {
        List<SList> listLikeById = listService.findListLikeById(listLikeId);
        return Result.success(listLikeById);
    }

    /**
     * 新建歌单
     *
     * @param newList
     * @return
     */
    @PostMapping("insertList")
    @ApiOperation(value = "新建歌单")
    public Result insertList(@RequestBody NewList newList) {
        System.out.println(newList);
        listService.insertNewList(newList);
        return Result.success("创建成功");
    }

    /**
     * 加入喜欢
     *
     * @param userId
     * @param listId
     * @return
     */
    @RequestMapping("insertLike")
    @ApiOperation(value = "加入喜欢")
    public Result insertLike(Integer userId, Integer listId) {
        LikeUserVo guanxiByUserList = listService.findGuanxiByUserList(userId, listId);
        if (guanxiByUserList == null) {
            listService.insertLike(userId, listId);
            return Result.success("收藏成功");
        } else {
            return Result.fail(4000, "已经收藏过了");
        }
    }

    /**
     * 歌曲添加进入歌单
     *
     * @param listId
     * @param songId
     * @return
     */
    @RequestMapping("insertListSong")
    @ApiOperation(value = "歌曲添加进入歌单")
    public Result insertListSong(Integer listId, Integer songId) {
        List_SongVo listSongVo = listService.findListSongVo(listId, songId);
        if (listSongVo == null) {
            listService.insertSongToList(listId, songId);
            return Result.success("添加成功");
        } else {
            return Result.fail(400, "已经添加过了");
        }
    }

    /**
     * 歌单列表
     *
     * @param current
     * @param size
     * @param listTittle
     * @return
     */
    @GetMapping("/songList")
    @ApiOperation(value = "歌单列表")
    public Result songList(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String listTittle
    ) {
        System.out.println("gogogo");
        IPage<Singer> page = listServ.page(new Page(current, size), new QueryWrapper<list>()
                .like(StrUtil.isNotBlank(listTittle), "listTittle", listTittle));
        return Result.success(page);
    }

    /**
     * 通过listId来删除歌单
     *
     * @param listId
     * @return
     */
    @GetMapping("/deleteListById")
    @ApiOperation(value = "通过listId来删除歌单")
    public Result deleteListById(
            @RequestParam Integer listId
    ) {
        listService.deleteSongListBylistId(listId);
        listService.deleteLikeBylistId(listId);
        listService.deleteListByListId(listId);
        return Result.success("成功");
    }

    /**
     * 更新歌单的图片
     *
     * @param listPhoto
     * @param listId
     * @return
     */
    @GetMapping("/updatePhoto")
    @ApiOperation(value = "更新歌单的图片")
    public Result UpdatePersonPhoto(@RequestParam String listPhoto, @RequestParam Integer listId) {
        listService.updateListPhoto(listPhoto, listId);
        return Result.success("修改成功");
    }

    @GetMapping("/findByListId")
    @ApiOperation(value = "通过listId来查找list")
    public Result findByListId(@RequestParam Integer listId) {
        SList slistByListId = listService.findSlistByListId(listId);
        return Result.success(slistByListId);
    }

    @GetMapping("/findListInfo")
    @ApiOperation(value = "根据listId来查找集合")
    public Result findSongsBylistId(@RequestParam Integer listId) {
        Object songByListId = listService.findSongByListId(listId);
        return Result.success(songByListId);
    }

    @GetMapping("/deleteListInfoSong")
    @ApiOperation("删除歌曲")
    public Result deleteListInfoSong(
            @RequestParam Integer listId,
            @RequestParam Integer songId
    ) {
        listService.deleteListInfoSong(songId, listId);
        return Result.success("修改成功");
    }

    @PostMapping("/updateListInfo")
    @ApiOperation(value = "更新集合信息")
    public Result updateListInfo(
            @RequestBody SList list
    ) {
        System.out.println(list);
        listService.updateListInfo(list);
        return Result.success("修改成功");
    }

    @GetMapping("/songListInsert")
    @ApiOperation(value = "吧歌曲加入到集合中")
    public Result songListInsert(
            @RequestParam String songName
    ) {
        List<Song> songListInsert = listService.findSongListInsert(songName);
        return Result.success(songListInsert);
    }

    @GetMapping("/songToInsertList")
    @ApiOperation(value = "加入歌曲list")
    public Result songToInsertList(
            @RequestParam Integer listId,
            @RequestParam Integer songId
    ) {
        listService.songToInsertList(listId, songId);
        return Result.success("修改成功");
    }

    @GetMapping("findListStyle")
    @ApiOperation(value = "查找歌曲风格比例")
    public Result findListStyle(){
        return Result.success(listService.findListStyle());
    }
}
